package com.gali.mall.service.impl;

import com.gali.mall.common.Const;
import com.gali.mall.exception.GaLiException;
import com.gali.mall.exception.GaLiExceptionEnum;
import com.gali.mall.model.dao.CartMapper;
import com.gali.mall.model.dao.ProductMapper;
import com.gali.mall.model.pojo.Cart;
import com.gali.mall.model.pojo.Product;
import com.gali.mall.model.vo.CartVO;
import com.gali.mall.service.CartService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author gali
 * @date 2022/4/21 20:04
 */
@Service
public class CartServiceImpl implements CartService {
    @Autowired
    private CartMapper cartMapper;
    @Autowired
    private ProductMapper productMapper;
    /**
     * 添加商品至购物车
     *
     * @param cart
     * @return
     */
    @Override
    public List<CartVO> addCart(Cart cart) {
        checkProductInfo(cart);
        Cart cart1 = cartMapper.selectCart(cart.getUserId(), cart.getProductId());
        if (cart1!=null){
            cart1.setQuantity(cart1.getQuantity()+cart.getQuantity());
            cart1.setSelected(Const.ProductSelected.SELECTED);
            cartMapper.updateByPrimaryKeySelective(cart1);
        }else {
            cart.setSelected(Const.ProductSelected.SELECTED);
            cartMapper.insertSelective(cart);
        }
        return this.list(cart.getUserId());
    }
   private void checkProductInfo(Cart cart){
       Product product = productMapper.selectByPrimaryKey(cart.getProductId());
       if (product==null||product.getStatus().equals(Const.ProductStatus.NO_STATUS)){
           throw new GaLiException(GaLiExceptionEnum.PRODUCT_NO_EXIST);
       }
       if (cart.getQuantity()>product.getStock()){
           throw new GaLiException(GaLiExceptionEnum.QUANTITY_NOT_GET);
       }
   }
   @Override
   public List<CartVO> list(Integer userId){
       List<CartVO> cartVOS = cartMapper.selectList(userId);
       for (int i = 0; i < cartVOS.size(); i++) {
           CartVO cartVO = cartVOS.get(i);
           cartVO.setTotalPrice(cartVO.getQuantity()*cartVO.getTotalPrice());
       }
       return cartVOS;
   }
    /**
     * 删除购物车的某个商品
     *
     * @param id
     * @return
     */
    @Override
    public boolean removeCart(Integer id) {
        return cartMapper.deleteByPrimaryKey(id)>0;
    }

    /**
     * 更新购物车
     *
     * @param cart
     * @return
     */
    @Override
    public boolean reviseCart(Cart cart) {
        return cartMapper.updateByPrimaryKeySelective(cart)>0;
    }

    /**
     * 查询全部商品
     *
     * @return
     */
    @Override
    public PageInfo findAll(Integer PageNumber, Integer pageSize) {
        PageHelper.startPage(PageNumber,pageSize,"id");
        List<Cart> list =cartMapper.selectAll();
        PageInfo pageInfo = new PageInfo(list);
        return pageInfo;
    }

    /**
     * 通过id查询商品
     *
     * @param id
     * @return
     */
    @Override
    public Cart findById(Integer id) {
        return cartMapper.selectByPrimaryKey(id);
    }

    /**
     * 批量选中商品
     *
     * @param
     * @param selected
     * @return
     */
    @Override
    public List<CartVO> updates(Integer userId,Integer productId,Integer selected) {
        int i = cartMapper.updateBrachSelected(userId, productId, selected);
        if (i<0){
          throw  new GaLiException(GaLiExceptionEnum.UPDATE_ERROR);
        }
        return this.list(userId);
    }
}
